
using Core;
using FairyGUI;
using Main;
using System;

public class MainWindow : FGUIWindow<UI_Main>
{
    private int[] ids = new int[3];

    public InventoryWidget inventoryWidget { get; private set; }
    public RoleInfoWidget roleInfoWidget { get; private set; }

    protected override async void OnEndLoad(params object[] args)
    {
        await this.CreateWidget<JoystickWidget, UI_Joystick>(m_root.m_joystick);
        await this.CreateWidget<TouchWidget, GObject>(m_root.m_touch);
        inventoryWidget = await this.CreateWidget<InventoryWidget, UI_Inventory>(m_root.m_inventory);
        roleInfoWidget = await this.CreateWidget<RoleInfoWidget, UI_RoleInfo>(m_root.m_roleInfo);
        roleInfoWidget.Bind(Player.Instance.GetParent<Unit>());

        m_root.m_list.itemRenderer = (index, gobject) =>
        {
            var card = gobject as UI_Card;
            var config = World.GetComponent<ConfigComponent>().GetConfig<RandomSkillCoinfig>(ids[index]);
            if (config == null)
            {
                card.visible = false;
                return;
            }
            card.m_name.text = config.name;
            card.m_describe.text = config.des;
            card.visible = true;
        };

        m_root.m_list.onClickItem.Add((content) =>
        {
            var index = m_root.m_list.GetChildIndex(content.data as GObject);
            if (index < 0 || index > ids.Length)
                return;
            var config = World.GetComponent<ConfigComponent>().GetConfig<RandomSkillCoinfig>(ids[index]);
            World.GetComponent<RandomSkillComponent>().SelectSkill(config.id);
            this.Hide();
        });
    }

    protected override void OnShown()
    {
        base.OnShown();

        for (int i = 0; i < ids.Length; i++)
        {
            ids[i] = World.GetComponent<RandomSkillComponent>().RandomSkill();
        }
        m_root.m_list.numItems = 3;
    }
}
